const form = document.querySelector('form')
form.onsubmit = function (e) {
  e.preventDefault()
  let rt = validEmail()
  if (rt) {
    submits()
  }
}

function submits() {
  console.log(email.value)
}

function validEmail() {
  let input = document.querySelector('#email')
  let email = input.value.trim()
  // 判断非空
  if (!email) {
    console.log('邮箱不能空')
    return false
  }
  // 判断@是否有
  let index = email.indexOf('@')
  if (index === -1) {
    console.log('邮箱不能没有@')
    return false
  }
  let lastIndex = email.lastIndexOf('@')
  if (lastIndex !== -1 && index !== lastIndex) {
    console.log('邮箱不能有两个@')
    return false
  }
  // 判断@不能在首尾
  if (email[0] === '@' || email[email.length - 1] === '@') {
    console.log('邮箱中@不能在首尾')
    return false
  }
  // 判断.
  let i = email.indexOf('.')
  if (i === -1) {
    console.log('邮箱至少有一个.符号')
    return false
  }
  if (i < index) {
    console.log('邮箱.符号要在@后面')
    return false
  }
  return true
}
